package edit;

import header.Items;
import header.RaceType;

import javax.swing.*;

import ui.GameLauncher;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;

// The frame for creating, editing and saving character
public class MainFrame {

	//Test2 t;
		FileHandler t;
		RaceType r2;
		int option;
		String[] rtype, ctype, shield, weaps, ran;
		JFrame frame;
		JTextField nameField;
		JRadioButton male, female;
		JComboBox raceCombo, classCombo, weapCombo, sCombo; 
		
		//public static void main(String[] args) {
		//	Test3 test = new Test3();
		//	try {
		//		Thread.sleep(3000);
		//	} catch (Exception e){
				// Do nothing
		//	}
//			test.setup();
//		}
		
		MainFrame(int choice) {
		//	t = new Test2();
		//	t.choosenType();
		//	option = t.getChoosen();
			option = choice;
			System.out.println("Test3 option: " + option);
			ctype = new String[]{"Barbarian", "Bard", "Cleric", "Druid", "Fighter", "Monk", "Paladin", "Ranger", "Rogue", "Sorcerer", "Wizard"};
			shield = new String[]{"Light", "Medium", "Heavy"};
			weaps = new String[]{"Bow n Arrow", "Dagger", "Spear", "Sword"};
			ran = new String[]{"Akuto", "William", "Lorenzo"};
			if (option == 1) {
				// Player is selected
				rtype = new String[]{"Human", "Elves", "Dwarves", "Halflings"};
			} else {
				// Opponent is selected
				rtype = new String[]{"Ghoul", "Medusa", "Mummy", "Serpent"};
			}
			setup();
		}
		
		MainFrame(RaceType r3) {
			r2 = new RaceType();
			r2.setArmor(r3.getArmor());
			r2.setClassType(r3.getClassType());
			if (r3.isGender())
				r2.setGender(true);
			else
				r2.setGender(false);
			r2.setName(r3.getName());
			r2.setRace(r3.getRace());
			r2.setWeapon(r3.getWeapon());
			String[] comp = new String[]{"Human", "Elves", "Dwarves", "Halflings"};
			String name = r2.getRace();
			System.out.println(r2.getRace());
			for(String val : comp) {
				if(val.equals(name)) {
					option = 1;
					break;
				} else
					option = 0;
			}
			System.out.println(option);
			ctype = new String[]{"Barbarian", "Bard", "Cleric", "Druid", "Fighter", "Monk", "Paladin", "Ranger", "Rogue", "Sorcerer", "Wizard"};
			shield = new String[]{"Light", "Medium", "Heavy"};
			weaps = new String[]{"Bow n Arrow", "Dagger", "Spear", "Sword"};
			ran = new String[]{"Akuto", "William", "Lorenzo"};
			if (option == 1) {
				// Player is selected
				rtype = new String[]{"Human", "Elves", "Dwarves", "Halflings"};
			} else {
				// Opponent is selected
				rtype = new String[]{"Ghoul", "Medusa", "Mummy", "Serpent"};
			}
			setup();
		}
		
		
		// To select all necessary information when RaceType object is passed
		public void fix() {
			if(r2 != null) {
				String cname = r2.getName().replace(".xml", "");
				nameField.setText(cname);
				if(r2.isGender())
					male.setSelected(true);
				else
					female.setSelected(true);
				// Race
				int counter = 0;
				String val;
				for(int i = 0; i < rtype.length; i++){
					val = rtype[i];
					if(val.equals(r2.getRace()))
						counter = i;
				}
				raceCombo.setSelectedIndex(counter);
				//Class
				for(int i = 0; i < ctype.length; i++){
					val = ctype[i];
					if(val.equals(r2.getClassType()))
						counter = i;
				}
				classCombo.setSelectedIndex(counter);
				//Shield
				for(int i = 0; i < shield.length; i++){
					val = shield[i];
					if(val.equals(r2.getArmor().getName()))
						counter = i;
				}
				sCombo.setSelectedIndex(counter);
				//Weapon
				for(int i = 0; i < weaps.length; i++){
					val = weaps[i];
					if(val.equals(r2.getWeapon().getName()))
						counter = i;
				}
				weapCombo.setSelectedIndex(counter);
			}
		}
		
		// The frame is created here along with everything else
		public void setup() {
			frame = new JFrame();
			// Name and Text Field
			JPanel north = new JPanel();
			JLabel name = new JLabel("Name: ");
			nameField = new JTextField(11);
			north.add(name);
			north.add(nameField);
			frame.getContentPane().add(BorderLayout.NORTH, north);
			
			JPanel center = new JPanel();
			center.setLayout(new BoxLayout(center, BoxLayout.Y_AXIS));
			// Radio Buttons for gender
			JPanel c1 = new JPanel();
			male = new JRadioButton("Male");
			female = new JRadioButton("Female");
			ButtonGroup b1 = new ButtonGroup();
			b1.add(male);
			b1.add(female);
			c1.add(male);
			c1.add(female);
			center.add(c1);
			
			// ComboBox for Race, Class, Weapons and Armor
			// Race
			JPanel c2 = new JPanel();
			c2.setLayout(new BoxLayout(c2, BoxLayout.Y_AXIS));
			JPanel c2a = new JPanel();
			JLabel race = new JLabel("Race: ");
			DefaultComboBoxModel r1 = new DefaultComboBoxModel();
			for(String ract : rtype)
					r1.addElement(ract);
			raceCombo = new JComboBox(r1);
			JScrollPane raceScroll = new JScrollPane(raceCombo);
			c2a.add(race);
			c2a.add(raceScroll);
			
			// Class
			JPanel c2b = new JPanel();
			JLabel clas = new JLabel("Class: ");
			DefaultComboBoxModel r2 = new DefaultComboBoxModel();
			for(String cltype : ctype)
					r2.addElement(cltype);
			classCombo = new JComboBox(r2);
			JScrollPane classScroll = new JScrollPane(classCombo);
			c2b.add(clas);
			c2b.add(classScroll);
			
			// Weapons
			JPanel c2c = new JPanel();
			JLabel w = new JLabel("Weapons: ");
			DefaultComboBoxModel r3 = new DefaultComboBoxModel();
			for(String weaptype : weaps)
					r3.addElement(weaptype);
			weapCombo = new JComboBox(r3);
			JScrollPane weapScroll = new JScrollPane(weapCombo);
			c2c.add(w);
			c2c.add(weapScroll);
			
			// Armor
			JPanel c2d = new JPanel();
			JLabel s = new JLabel("Armor: ");
			DefaultComboBoxModel r4 = new DefaultComboBoxModel();
			for(String stype : shield)
					r4.addElement(stype);
			sCombo = new JComboBox(r4);
			JScrollPane sScroll = new JScrollPane(sCombo);
			c2d.add(s);
			c2d.add(sScroll);
			
			c2.add(c2a);
			c2.add(c2b);
			c2.add(c2c);
			c2.add(c2d);
			
		//	center.add(c1);
			center.add(c2);
			frame.getContentPane().add(BorderLayout.CENTER, center);
			
			//Buttons
			JPanel south = new JPanel();
			JButton edit = new JButton("Edit");
			JButton save = new JButton("Save");
			JButton def = new JButton("Default");
			south.add(edit);
			south.add(save);
			south.add(def);
			edit.addActionListener(new editListener());
			save.addActionListener(new saveListener());
			def.addActionListener(new defListener());
			frame.getContentPane().add(BorderLayout.SOUTH, south);
			
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			frame.setSize(300, 400);
			frame.setVisible(true);
			fix();
			
		}
		
		//Class for edit button. Calls FileHanlder
		class editListener implements ActionListener {
			public void actionPerformed(ActionEvent e) {
				t = new FileHandler();
				frame.setVisible(false);
				frame.dispose();
			}
		}
		
		// Save button handler. Records all information and saves it in a file specified by Name JTextField
		class saveListener implements ActionListener {
			public void actionPerformed(ActionEvent e){
				if (r2 == null)
					r2 = new RaceType();
				System.out.println(nameField.getText());
				r2.setName(nameField.getText());
				if(male.isSelected()) {
					System.out.println("Male is selected");
					r2.setGender(true);
				} else {
					System.out.println("Female is selected");
					r2.setGender(false);
				}
				if(raceCombo.getSelectedIndex() != -1) {
					System.out.println(raceCombo.getItemAt(raceCombo.getSelectedIndex()));
					String race = (String) raceCombo.getItemAt(raceCombo.getSelectedIndex());
					r2.setRace(race);
				}
				if(classCombo.getSelectedIndex() != -1) {
					System.out.println(classCombo.getItemAt(classCombo.getSelectedIndex()));
					String cl = (String) classCombo.getItemAt(classCombo.getSelectedIndex());
					r2.setClassType(cl);
				}
				if(weapCombo.getSelectedIndex() != -1) {
					System.out.println(weapCombo.getItemAt(weapCombo.getSelectedIndex()));
					String wea = (String) weapCombo.getItemAt(weapCombo.getSelectedIndex());
					r2.setWeapon(new Items(0, wea));
				}
				if(sCombo.getSelectedIndex() != -1) {
					System.out.println(sCombo.getItemAt(sCombo.getSelectedIndex()));
					String s = (String) sCombo.getItemAt(sCombo.getSelectedIndex());
					r2.setArmor(new Items(0, s));
				}
				try {
					FileOutputStream fout = new FileOutputStream(r2.getName().concat(".xml"));
					ObjectOutputStream oos = new ObjectOutputStream(fout);
					oos.writeObject(r2);
					oos.close();
					System.out.println("Object Written");
				} catch (Exception ex) {
					ex.printStackTrace();
				}
				
				frame.dispose();
				
				int mc = JOptionPane.INFORMATION_MESSAGE;
				String message1 = r2.getName().concat(".xml");
				String message2 = "File saved: ";
				String message = message2 + message1;
				JOptionPane.showMessageDialog(null, message, "Alert", mc);
				
				GameLauncher gm = new GameLauncher();
			}
		}
		
		// Default button listener. When clicks changes all to random, but fixes class to Fighter
		class defListener implements ActionListener {
			public void actionPerformed(ActionEvent e) {
				int idx = new Random().nextInt(ran.length);
				nameField.setText(ran[idx]);
				idx = new Random().nextInt(2) - 1;
				if (idx == 1)
					male.setSelected(true);
				else
					female.setSelected(true);
				
				idx = new Random().nextInt(rtype.length);
				raceCombo.setSelectedIndex(idx);
				
				classCombo.setSelectedIndex(4);
				
				idx = new Random().nextInt(weaps.length);
				weapCombo.setSelectedIndex(idx);
				
				idx = new Random().nextInt(shield.length);
				sCombo.setSelectedIndex(idx);
			}
		}

}
